package com.kestrel.space.core.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.kestrel.space.common.utils.Generator;
import com.kestrel.space.common.utils.LargePage;
import com.kestrel.space.core.mapper.LikeMapper;
import com.kestrel.space.core.model.Like;
import com.kestrel.space.core.query.LikeQuery;
import com.kestrel.space.core.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author yebolin
 * @date 2024/9/6
 */
@Service
public class LikeServiceImpl implements LikeService {
    @Autowired
    private LikeMapper likeMapper;

    @Override
    public List<Like> listContent(LikeQuery query, LargePage page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
        }
        Like like = BeanUtil.copyProperties(query, Like.class);
        return likeMapper.select(like);
    }

    @Override
    public String saveOrUpdate(Like like) {
        String message = "插入成功";
        if (like.getId() == null) {
            like.setId(Generator.generateId());
            likeMapper.insert(like);
        } else {
            message = "更新成功";
            likeMapper.updateByPrimaryKeySelective(like);
        }
        return message;
    }

    @Override
    public String batchSaveOrUpdate(List<Like> likeList) {
        String message = "批量插入成功";
        if (CollectionUtils.isEmpty(likeList)) {
            return "集合为空";
        }
        if(likeList.get(0).getId() == null){
            for (Like like : likeList) {
                like.setId(Generator.generateId());
                likeMapper.insert(like);
            }
        } else {
            message = "批量更新成功";
            for (Like like : likeList) {
                like.setId(Generator.generateId());
                likeMapper.updateByPrimaryKey(like);
            }
        }
        return message;
    }
}
